package com.reggie.controller;

import com.reggie.common.R;
import com.reggie.common.ThreadLocal;
import com.reggie.dto.DishDto;
import com.reggie.dto.OrdersDto;
import com.reggie.pojo.AddressBook;
import com.reggie.pojo.Dish;
import com.reggie.pojo.Setmeal;
import com.reggie.pojo.ShoppingCart;
import com.reggie.service.impl.ShoppingCartServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingController {
    @Autowired
    private ShoppingCartServiceImpl shoppingCartService;



    @GetMapping("/list")
    public R<List<ShoppingCart>> shoppingCart(HttpServletRequest request) {
        return R.success(shoppingCartService.selectAll(request));
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        Long user_id = (Long) request.getSession().getAttribute("user");
        System.out.println(user_id);
        shoppingCart.setUserId(user_id);
        shoppingCartService.add(shoppingCart);
        return R.success("添加成功");
    }

    @PostMapping("/sub")
    public R<String> subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        Boolean flag = shoppingCartService.subShoppingCart(shoppingCart);
        if (flag) {
            return R.success("删掉一个商品");
        } else {
            return R.success("删除失败");
        }
    }

    @DeleteMapping("/clean")
    public R<String> deleteAll(){
        Boolean flag = shoppingCartService.deleteAll();
        return flag ? R.success("清空成功！") : R.error("清空失败");
    }
}
